package com.symbio.ishenapi.background.controller;

import com.symbio.ishenapi.background.oss.OSSClientUtil;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@RequestMapping("/uploadController")
public class uploadController extends BaseController {


    /**
     * 上传图片，返回url地址
     */
    public void upload(){
        File file = getParamFile();

        //file.getName().replaceAll("\\\\","/");
        System.out.println("=============="+file.getPath().replaceAll("\\\\","/").toString());
        /*String path = getPara("path");*/
    /*    String picUrl = "";
        //String url = "D:\\img\\15409589411.png,D:\\img\\512450790949615937.png";
        String[] str = path.split(",");
        for(int i = 0;i<str.length;i++){*/
           // File file = new File(path);
            FileInputStream in_file = null;
            try {
                in_file = new FileInputStream(file.getPath().replaceAll("\\\\","/").toString());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 转 MultipartFile
            MultipartFile multi = null;
            try {
                multi = new MockMultipartFile(file.getName(), file.getName(),
                        ContentType.APPLICATION_OCTET_STREAM.toString(), in_file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (multi == null || multi.getSize() <= 0) {
                //throw new ImgException("file不能为空");
            }
            OSSClientUtil ossClient = new OSSClientUtil();
            String name = ossClient.uploadImg2Oss(multi,1L);
            String imgUrl = ossClient.getImgUrl(name);
            String[] split = imgUrl.split("\\?");
            //System.out.println(imgUrl);
            System.out.println("111===========" + split[0]);
            /*if (picUrl.length() <= 0) {
                picUrl += split[0];
            } else {
                picUrl += "," + split[0];
            }
        }*/
        System.out.println(split[0]);
        renderJsonRet(split[0]);
    }


    /**
     * 上传多图片，返回url地址
     */
    public void uploads(){

        List<String> urls = new ArrayList<>();
        List<File> files = getParamFiles();
        for(int i=0;i<files.size();i++){
            File file = files.get(i);
            FileInputStream in_file = null;
            try {
                in_file = new FileInputStream(file.getPath().replaceAll("\\\\","/").toString());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 转 MultipartFile
            MultipartFile multi = null;
            try {
                multi = new MockMultipartFile(file.getName(), file.getName(),
                        ContentType.APPLICATION_OCTET_STREAM.toString(), in_file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (multi == null || multi.getSize() <= 0) {
                //throw new ImgException("file不能为空");
            }
            OSSClientUtil ossClient = new OSSClientUtil();
            String name = ossClient.uploadImg2Oss(multi,1L);
            String imgUrl = ossClient.getImgUrl(name);
            String[] split = imgUrl.split("\\?");
            urls.add(split[0]);
        }

        renderJsonRet(urls);
    }



    /**
     * 上传语音，返回url地址
     */
    public void uploadVoice(){
        File file = getParamFile();

        //file.getName().replaceAll("\\\\","/");
        System.out.println("=============="+file.getPath().replaceAll("\\\\","/").toString());
        /*String path = getPara("path");*/
    /*    String picUrl = "";
        //String url = "D:\\img\\15409589411.png,D:\\img\\512450790949615937.png";
        String[] str = path.split(",");
        for(int i = 0;i<str.length;i++){*/
        // File file = new File(path);
        FileInputStream in_file = null;
        try {
            in_file = new FileInputStream(file.getPath().replaceAll("\\\\","/").toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 转 MultipartFile
        MultipartFile multi = null;
        try {
            multi = new MockMultipartFile(file.getName(), file.getName(),
                    ContentType.APPLICATION_OCTET_STREAM.toString(), in_file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (multi == null || multi.getSize() <= 0) {
            //throw new ImgException("file不能为空");
        }
        OSSClientUtil ossClient = new OSSClientUtil();
        String name = ossClient.uploadImg2Oss(multi,2L);
        String imgUrl = ossClient.getImgUrl(name);
        String[] split = imgUrl.split("\\?");
        //System.out.println(imgUrl);
        System.out.println("111===========" + split[0]);
            /*if (picUrl.length() <= 0) {
                picUrl += split[0];
            } else {
                picUrl += "," + split[0];
            }
        }*/
        System.out.println(split[0]);
        renderJsonRet(split[0]);
    }
}
